package com.hanson.demo.queue.memory;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import com.hanson.demo.actors.Message;
import com.hanson.demo.queue.TbQueueCallback;
import com.hanson.demo.queue.TbQueueProducer;

@Component
@ConditionalOnProperty(prefix = "queue", value = "type", havingValue = "memory")
public class InMemoryTbQueueProducer<T extends Message> implements TbQueueProducer<T> {

    private final InMemoryStorage storage = InMemoryStorage.getInstance();

    @Override
    public void init() {

    }

    @Override
    public void send(String tpi, T msg, TbQueueCallback callback) {
        boolean result = storage.put(tpi, msg);
        if (result) {
            if (callback != null) {
                callback.onSuccess(null);
            }
        } else {
            if (callback != null) {
                callback.onFailure(new RuntimeException("Failure add msg to InMemoryQueue"));
            }
        }
    }

    @Override
    public void stop() {

    }

	@Override
	public String getDefaultTopic() {
		return null;
	}
}
